/*  This file is part of LibGChart.
 *
 * AUTHORS
 *       Sascha Dewald        <sascha.dewald@googlemail.com>
 *
 * Copyright (C) 2009 Sascha Dewald
 *
 *  
 *  
 *  LibGChart is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Foobar is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with LibGChart.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

using System;
using System.Collections;

namespace LibGChart
{
	public class GSeriesList : System.Collections.IEnumerable
	{
		private GAxis xaxis, yaxis;
		
		
		private ArrayList seriesList = new ArrayList();
		
		/**
		 * Default Constructor
		 */
		public GSeriesList(GAxis xaxis, GAxis yaxis)
		{
			this.xaxis = xaxis;
			this.yaxis = yaxis;
		}
		
		/** 
		 * Add a new Series
		 */
		public void Add(GSeries series)
		{
			seriesList.Add(series);
			series.xaxis = xaxis;
			series.yaxis = yaxis;
			series.calc_real_coords_xy();
		}
		
		/**
		 * Insert a Series
		 */
		public void Insert(int index, GSeries series)
		{
			seriesList.Insert(index, series);
		}

		/*
		 * Removes a Series
		 */
		public void Remove(GSeries series)
		{
			seriesList.Remove(series);
		}
		
		public int Count
		{
			get { return seriesList.Count; }
		}
		
		/**
		 *  Get/set Series at given index
		 */
		public GSeries this[int index]
		{
			get { return (GSeries)seriesList[index]; } //FIXME: should i better use "as"
			set { seriesList[index] = value; }
		}
		
		public IEnumerator GetEnumerator()
		{
			return seriesList.GetEnumerator();
		}
		
		
		private void calc_xy()
		{
			// Iterate over Series ArrayList
			IEnumerator seriesListEnumerator = GetEnumerator(); 
			
			seriesListEnumerator.Reset(); // FIXME: properly not needed
			
			while ( seriesListEnumerator.MoveNext()) 
			{
				(seriesListEnumerator.Current as GSeries).calc_real_coords_xy();
			}
		}
		
		/**
		 * function calc_series, calculate the series coords
		 */
		internal void calc_x()
		{
			// Iterate over Series ArrayList
			IEnumerator seriesListEnumerator = GetEnumerator(); 
			
			seriesListEnumerator.Reset(); // FIXME: properly not needed
			
			while ( seriesListEnumerator.MoveNext()) 
			{
				(seriesListEnumerator.Current as GSeries).calc_real_coords_x();
			} 
		}
		
		/**
		 * function calc_series, calculate the series coords
		 */
		internal void calc_y()
		{
			// Iterate over Series ArrayList
			IEnumerator seriesListEnumerator = GetEnumerator(); 
			
			seriesListEnumerator.Reset(); // FIXME: properly not needed
			
			while ( seriesListEnumerator.MoveNext()) 
			{
				(seriesListEnumerator.Current as GSeries).calc_real_coords_y();
			} 
		}
		
		/**
		 * function drawSeries, draw the Series
		 */
		internal void draw(Cairo.Context cr, double chartStartX, double chartStartY, double chartWidth, double chartHeight)
		{
			cr.Save();
			
			cr.Rectangle(chartStartX, chartStartY, chartWidth, chartHeight);
			cr.Clip();
			
			// Iterate over Series ArrayList
			// FIXME: there should be a better way for a List
			IEnumerator seriesListEnumerator = GetEnumerator(); 
			
			seriesListEnumerator.Reset(); // FIXME: properly not needed
			
			while ( seriesListEnumerator.MoveNext()) 
			{
				(seriesListEnumerator.Current as GSeries).draw(cr);
			}
			
			cr.Restore();
		}
	}
}
